package net.lengwang.iot.middleware.network;

import net.lengwang.iot.middleware.packet.PacketAnalyzer;
import net.lengwang.iot.middleware.packet.PacketHandler;
import net.lengwang.iot.middleware.packet.PacketType;
import net.lengwang.iot.middleware.packet.ParsedPacket;
import net.lengwang.iot.middleware.packet.request.PacketParser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultPacketHandler implements PacketHandler {
    private static final Logger logger = LoggerFactory.getLogger(DefaultPacketHandler.class);
    private PacketAnalyzer packetAnalyzer;
    private CommonServiceInterface handshakeService;

    public DefaultPacketHandler(CommonServiceInterface handCommonService) {
        this.packetAnalyzer = new PacketAnalyzer();
        this.handshakeService = handCommonService;
    }

    @Override
    // 处理上传的数据
    public void handlePacket(ParsedPacket parsedPacket) {
        this.handshakeService.handlePacket(parsedPacket);
        // 使用packetAnalyzer实例调用方法
        String packetType = parsedPacket.getCommand();
        System.out.println("（2）数据包类型:" + packetType);
        switch (packetType) {
            case PacketType.BEBE_GATEWAY_HANDSHAKE:
                this.handshakeService.handlePacket(parsedPacket);
                break;
            case PacketType.BEBE_GATEWAY_TIME_SYNC:
                this.handshakeService.handlePacket(parsedPacket);
                break;
            default:
                logger.error("packet:" + packetType);
                break;
        }
    }
}
